package com.cqeec.myblog.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqeec.myblog.model.Article;
import com.cqeec.myblog.model.DataDictionary;
import com.cqeec.myblog.service.ArticleService;
import com.cqeec.myblog.service.ArticleServiceImpl;

@WebServlet("/article/index.html")
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1255547692130877398L;
	private int pageSize = 2;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int curPageNumber;
		String page = request.getParameter("page");
		if (page == null || "".equals(page)) {
			curPageNumber = 1;
		} else {
			curPageNumber = Integer.parseInt(page);
		}
		// 创建文章服务类对象
		ArticleService articleService = new ArticleServiceImpl();
		List<Article> list = null;
		List<DataDictionary> atList = null;
		int pages = 0; // 总页数
		try {
			// 获取的文章
			// list = articleService.getAllArticle(); //获取所有文章
			// 获取总页数
			int totalNumber = articleService.getRecordsTotalNumber();
			if (totalNumber % pageSize == 0) {
				pages = totalNumber / pageSize;
			} else {
				pages = totalNumber / pageSize + 1;
			}
			list = articleService.getArticleByPage(pageSize, curPageNumber);
			// 获取的文章类型
			atList = articleService.getArticleType();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 将数据带到页面
		request.setAttribute("pages", pages);
		request.setAttribute("curPage", curPageNumber);
		request.setAttribute("list", list);
		request.setAttribute("atList", atList);
		request.getRequestDispatcher("../article.jsp").forward(request, response);
	}

}
